package post_to_in_pkg.node;
import post_to_in_pkg.lexer.Token;
import post_to_in_pkg.parser.Type;

public class OperandNode extends Node{
	 private Type type;
	 private Token operand;
	 private boolean is_const_str=false;
	public String[] code={"",""}; //implement the additivity of "codes"
	// String[0] stand for the codes occur before the nested block
	// String[1] stand for the synthesized codes occur inside the nested block
	 private boolean referenced;

	public OperandNode(Token tok,Type T) {
		type=T; operand=tok;
	}
	
	public String toString() {
		return " "+operand.toString()+"; ";
	}
	
	public Token getToken() {
		return operand;
	}
	
	public boolean isReturnVoid() {
		return false;
	}
	
	public boolean isTempNode() {
		return type==Type.TEMPVAR;
	}
	
	public void changeToConstStrOpnode(){
		is_const_str=true;
	}

	public boolean isConstStr(){
		return is_const_str;
	}

	public String getName() {
		return operand.toString();
	}

	public void setReferenced(boolean yes){
		referenced=yes;
	}
	public boolean isReferenced(){
		return referenced;
	}
	@Override
	public boolean hasChild() {
		// TODO Auto-generated method stub
		// if this is a TempNode it means that this node is original from a method-return value 
		if(this instanceof TempNode) {
			return true;
		}
		else
			return false;
	}

//	@Override
//	public void printChild(StringBuffer content) {
//		// TODO Auto-generated method stub
//		if(hasChild()) {
//			((TempNode)this).printChild(content);
//		}
//		else {
//			content=content.insert(0,toString());
//		}
//	}

	//two cases of code generation
	//1. only an operand staying on top of the stack
	//2. this operand is being used by other operator node
	//how to distinguish which of one is meant to be
	public String[] generateCodes(){
		if(isReferenced()){
			code[1]=getName();
		}
		else{
			code[1]=getName()+"; ";
		}
		return code;
	}

	public String[] generateCodes(int nestLevel){
		return ((OperandNode)this).generateCodes();
	}
}
